home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_devices / rkrm_devices.lha / Printer / Demo_Dump.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  15KB  |  314 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  ***************************************************************************
  27.  *
  28.  * Demo_Dump.c
  29.  *
  30.  * Simple example of dumping a rastport to the printer, changing
  31.  * printer preferences programmatically and handling error codes.
  32.  *
  33.  * Compile with SAS C 5.10a. lc -cfist -v -L Demo_Dump
  34.  *
  35.  * Run from CLI only
  36.  */
  37.  
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <exec/ports.h>
  41. #include <devices/printer.h>
  42. #include <devices/prtbase.h>
  43. #include <dos/dos.h>
  44. #include <intuition/intuition.h>
  45. #include <intuition/screens.h>
  46. #include <graphics/displayinfo.h>
  47.  
  48. #include <clib/exec_protos.h>
  49. #include <clib/alib_protos.h>
  50. #include <clib/alib_stdio_protos.h>
  51. #include <clib/graphics_protos.h>
  52. #include <clib/intuition_protos.h>
  53.  
  54. struct IntuitionBase *IntuitionBase;
  55. struct GfxBase *GfxBase;
  56.  
  57. union printerIO
  58. {
  59.     struct IOStdReq    ios;
  60.     struct IODRPReq    iodrp;
  61.     struct IOPrtCmdReq iopc;
  62. };
  63.  
  64. struct EasyStruct reqES =
  65. {
  66.     sizeof(struct EasyStruct), 0, "DemoDump",
  67.     "%s",
  68.     NULL,
  69. };
  70.  
  71. /* Possible printer.device and I/O errors */
  72. static UBYTE *ErrorText[] =
  73. {
  74.     "PDERR_NOERR",
  75.     "PDERR_CANCEL",
  76.     "PDERR_NOTGRAPHICS",
  77.     "INVERTHAM",        /* OBSOLETE */
  78.     "BADDIMENSION",
  79.     "DIMENSIONOVFLOW",    /* OBSOLETE */
  80.     "INTERNALMEMORY",
  81.     "BUFFERMEMORY",
  82.     /* IO_ERRs */
  83.     "IOERR_OPENFAIL",
  84.     "IOERR_ABORTED",
  85.     "IOERR_NOCMD",
  86.     "IOERR_BADLENGTH"
  87. };
  88.  
  89. /* Requester Action text */
  90. static UBYTE *ActionText[] =
  91. {
  92.     "OK|CANCEL",
  93.     "Continue",
  94.     "Abort",
  95. };
  96.  
  97. #define OKCANCELTEXT 0
  98. #define CONTINUETEXT 1
  99. #define ABORTTEXT    2
  100.  
  101. VOID main(VOID);
  102.  
  103. VOID main(VOID)
  104. {
  105. struct MsgPort  *PrinterMP;
  106. union printerIO *PIO;
  107. struct PrinterData *PD;
  108. struct PrinterExtendedData *PED;
  109. struct Screen *pubscreen;
  110. struct ViewPort *vp;
  111. STRPTR textbuffer;
  112. LONG modeID, i,j;
  113. ULONG dcol[5], drow[5];
  114. ULONG signal;
  115.  
  116. /* Fails silently if not V37 or greater. Nice thing to do would be to put up
  117.  * a V33 requester of course.
  118.  */
  119.  
  120. /* Set up once */
  121. reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  122.  
  123. if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37))
  124.     {
  125.     /* Using graphics.library to get the displaymodeID of the public screen,
  126.      * which we'll pass to the printer.device.
  127.      */
  128.     if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37))
  129.         {
  130.         if (textbuffer = (STRPTR)AllocMem(256, MEMF_CLEAR))
  131.             {
  132.             /* Create non-public messageport. Since we depend on V37 already, we'll
  133.              * use the new Exec function.
  134.              */
  135.             if (PrinterMP = CreateMsgPort())
  136.                 {
  137.                 /* Allocate printerIO union */
  138.                 if (PIO = (union printerIO *)CreateExtIO(PrinterMP, sizeof(union printerIO)))
  139.                     {
  140.                     /* Open the printer.device */
  141.                     if (!(OpenDevice("printer.device",0,(struct IORequest *)PIO,0)))
  142.                         {
  143.                         /* Yahoo, we've got it.
  144.                          * We'll use the PrinterData structure to get to the the printer
  145.                          * preferences later on. The PrinterExtendedData structure will
  146.                          * reflect the changes we'll make to the preferences.
  147.                          */
  148.  
  149.                         PD = (struct PrinterData *)PIO->iodrp.io_Device;
  150.                         PED = (struct PrinterExtendedData *)&PD->pd_SegmentData->ps_PED;
  151.  
  152.                         /* We're all set. We'll grab the default public screen (normally
  153.                          * Workbench) and see what happens when we dump it with different
  154.                          * densities.
  155.                          * Next we'll put up a nice requester for the user and ask if
  156.                          * (s)he wants to actually do the dump.
  157.                          */
  158.  
  159.                         if (pubscreen = LockPubScreen(NULL))
  160.                             {
  161.                             vp = &(pubscreen->ViewPort);
  162.                             /* Use graphics.library/GetVPModeID() to get the ModeID of the screen. */
  163.                             if ((modeID = GetVPModeID(vp)) != INVALID_ID)
  164.                                 {
  165.                                 /* Seems we got a valid ModeID for the default public screen (surprise).
  166.                                  * Do some fake screen dumps with densities 1, 3, 5 and 7. Depending on
  167.                                  * the driver, one or more may be the same.
  168.                                  */
  169.  
  170.                                 /* Fill in those parts of the IODRPRequest which won't change */
  171.                                 PIO->iodrp.io_Command = PRD_DUMPRPORT;
  172.                                 PIO->iodrp.io_RastPort = &(pubscreen->RastPort);
  173.                                 PIO->iodrp.io_ColorMap = vp->ColorMap;
  174.                                 PIO->iodrp.io_Modes = modeID;
  175.                                 PIO->iodrp.io_SrcX = pubscreen->LeftEdge;
  176.                                 PIO->iodrp.io_SrcY = pubscreen->TopEdge;
  177.                                 PIO->iodrp.io_SrcWidth = pubscreen->Width;
  178.                                 PIO->iodrp.io_SrcHeight = pubscreen->Height;
  179.  
  180.                                 for (i = 1,j=0; i < 8; i+=2,j++)
  181.                                     {
  182.                                     /* On return these will contain the actual dump dimension */
  183.                                     PIO->iodrp.io_DestCols = 0;
  184.                                     PIO->iodrp.io_DestRows = 0;
  185.                                     /* We'll simply change our local copy of the
  186.                                      * Preferences structure. Likewise we could change
  187.                                      * all printer-related preferences.
  188.                                      */
  189.                                     PD->pd_Preferences.PrintDensity = i;
  190.                                     PIO->iodrp.io_Special = SPECIAL_NOPRINT|SPECIAL_ASPECT;
  191.  
  192.                                     /* No need to do asynchronous I/O here */
  193.                                     DoIO((struct IORequest *)PIO);
  194.  
  195.                                     if (PIO->iodrp.io_Error == 0)
  196.                                         {
  197.                                         dcol[j] = PIO->iodrp.io_DestCols;
  198.                                         drow[j] = PIO->iodrp.io_DestRows;
  199.                                         }
  200.                                     else
  201.                                         {
  202.                                         j = PIO->iodrp.io_Error;
  203.                                         if (j < 0)
  204.                                             j = j * -1 + 7;
  205.  
  206.                                         sprintf(textbuffer, "Error: %s\n", ErrorText[j]);
  207.                                         reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  208.                                         EasyRequest(NULL, &reqES, NULL, textbuffer);
  209.                                         break;
  210.                                         }
  211.                                     }
  212.                                 /* Simple, lazy way to check if we encountered any problems */
  213.                                 if (i == 9)
  214.                                     {
  215.                                     /* Build an 'intelligent' requester */
  216.                                     sprintf(textbuffer,
  217.                                         "%s: %5ld x %5ld\n%s: %5ld x %5ld\n%s: %5ld x%5ld\n%s: %5ld x %5ld\n\n%s",
  218.                                         "Density 1", dcol[0], drow[0],
  219.                                         "Density 3", dcol[1], drow[1],
  220.                                         "Density 5", dcol[2], drow[2],
  221.                                         "Density 7", dcol[3], drow[3],
  222.                                         "Print screen at highest density?");
  223.                                     reqES.es_GadgetFormat = ActionText[OKCANCELTEXT];
  224.  
  225.                                     /* Obviously the choice presented to the user here is a very
  226.                                      * simple one. To print or not to print. In a real life
  227.                                      * application, a requester could be presented, inviting
  228.                                      * the user to select density, aspect, dithering etc.
  229.                                      * The fun part is, of course, that the user can, to a certain
  230.                                      * degree, be informed about the effects of her/his selections.
  231.                                      */
  232.                                     if (EasyRequest(NULL, &reqES, NULL, textbuffer))
  233.                                         {
  234.                                         /* We've still got the density preference set to the highest
  235.                                          * density, so no need to change that.
  236.                                          * All we do here is re-initialize io_DestCols/Rows and remove
  237.                                          * the SPECIAL_NOPRINT flag from io_Special.
  238.                                          */
  239.                                         PIO->iodrp.io_DestCols = 0;
  240.                                         PIO->iodrp.io_DestRows = 0;
  241.                                         PIO->iodrp.io_Special &= ~SPECIAL_NOPRINT;
  242.  
  243.                                         /* Always give the user a change to abort.
  244.                                          * So we'll use SendIO(), instead of DoIO(), to be asynch and
  245.                                          * catch a possible user request to abort printing. Normally,
  246.                                          * the user would be presented with a nice, fat, ABORT requester.
  247.                                          * However, since this example doesn't even open a window, and is
  248.                                          * basically a 'GraphicDumpDefaultPubscreen' equivalent, we'll use
  249.                                          * CTRL-C as the user-abort. Besides that, got to keep it short.
  250.                                          */
  251.                                         SendIO((struct IORequest *)PIO);
  252.  
  253.                                         /* Now Wait() for either a user signal (CTRL-C) or a signal from
  254.                                          * the printer.device
  255.                                          */
  256.                                         signal = Wait(1 << PrinterMP->mp_SigBit | SIGBREAKF_CTRL_C);
  257.  
  258.                                         if (signal & SIGBREAKF_CTRL_C)
  259.                                             {
  260.                                             /* User wants to abort */
  261.                                             AbortIO((struct IORequest *)PIO);
  262.                                             WaitIO((struct IORequest *)PIO);
  263.                                             }
  264.  
  265.                                         if (signal & (1 << PrinterMP->mp_SigBit))
  266.                                             {
  267.                                             /* printer is either ready or an error has occurred */
  268.                                             /* Remove any messages */
  269.                                             while(GetMsg(PrinterMP));
  270.                                             }
  271.                                          /* Check for errors (in this case we count user-abort as an error) */
  272.                                         if (PIO->iodrp.io_Error != 0)
  273.                                             {
  274.                                             j = PIO->iodrp.io_Error;
  275.                                             if (j < 0)
  276.                                                 j = j * -1 + 7;
  277.                                             sprintf(textbuffer, "Error: %s\n", ErrorText[j]);
  278.                                             reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  279.                                             EasyRequest(NULL, &reqES, NULL, textbuffer);
  280.                                             }
  281.  
  282.                                         } /* else user doesn't want to print */
  283.                                     }
  284.                                 }
  285.                             else
  286.                                 /* Say what? */
  287.                                 EasyRequest(NULL, &reqES, NULL, "Invalid ModeID\n");
  288.                             UnlockPubScreen(NULL, pubscreen);
  289.                             }
  290.                         else
  291.                             EasyRequest(NULL, &reqES, NULL, "Can't lock Public Screen\n");
  292.  
  293.                         CloseDevice((struct IORequest *)PIO);
  294.                         }
  295.                     else
  296.                         EasyRequest(NULL, &reqES, NULL, "Can't open printer.device\n");
  297.  
  298.                     DeleteExtIO((struct IORequest *)PIO);
  299.                     }
  300.                 else
  301.                     EasyRequest(NULL, &reqES, NULL, "Can't create Extented I/O Request\n");
  302.                 DeleteMsgPort(PrinterMP);
  303.                 }
  304.             else
  305.                 EasyRequest(NULL, &reqES, NULL, "Can't create Message port\n");
  306.             /* else Out of memory? 256 BYTES? */
  307.             FreeMem(textbuffer,256);
  308.             }
  309.         CloseLibrary(GfxBase);
  310.         } /* else MAJOR confusion */
  311.     CloseLibrary(IntuitionBase);
  312.     }
  313. }
  314.